package com.woniu.learnhelloworld.controller;

import com.woniu.learnhelloworld.entity.TestBean2;
import com.woniu.learnhelloworld.entity.User;
import com.woniu.learnhelloworld.utils.SpringBeanRegisterUtil;
import com.woniu.learnhelloworld.utils.SpringContextUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * created  by woniu201
 * created in 2018/3/22
 */
@RestController
@CrossOrigin
public class HelloController {

/*********************************** GET ***********************************************/
    @RequestMapping(value = "/hello1", method= RequestMethod.GET)
    public String hello1(){
        //new TestBean2().test1();

        //注册bean
        SpringBeanRegisterUtil.registerBean("testbean2", TestBean2.class.getName());
        //获取bean
        ((TestBean2)SpringContextUtil.getBean("testbean2")).test2();

        return "hello world\r\nwangpengfei";
    }

    /**
     * URL路径中变量
     */
    @RequestMapping(value = "/hello2/{name}", method = RequestMethod.GET)
    public String hello2(@PathVariable String name){
        return "hello:" + name;
    }

    /**
     * URL路径变量中带点号，例：localhost:8080/hello/1.jgp
     */
    @RequestMapping(value = "/hello3/{name:.+}", method = RequestMethod.GET)
    public String hello3(@PathVariable String name){
        return "hello:" + name;
    }

    /**
     * URL路径中变量，例如：localhost:8080/hello/wwngpengfei?age=18&&addr=handan
     */
    @RequestMapping(value = "/hello4/{name}")
    public String hello4(@PathVariable String name, @RequestParam("age") int age, @RequestParam("addr") String addr){
        return "hello:" + name + "-" + String.valueOf(age) + "-" + addr;
    }


    /**
     * 返回JSON
     */
    @RequestMapping(value = "/hello5", method = RequestMethod.GET)
    public User hello5(){
        User user = new User();
        user.setAge(18);
        user.setName("wangpengfei");
        return user;
    }

/*********************************** POST ***********************************************/
    @RequestMapping(value = "/hello6", method = RequestMethod.POST)
    public String hello6(@Valid @RequestBody  User user, BindingResult bindingResult/*HttpServletRequest req*/){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
        }
        System.out.println(user.getName());
        System.out.println(user.getAge());
        //System.out.println("______:"+req.getParameter("name"));
        return "aaaaaaaaaaaaa";
    }

/*********************************** PUT ************************************************/
    @RequestMapping(value = "/hello7", method = RequestMethod.PUT)
    public void hello7(@RequestBody User user){
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }

/*********************************** DELETE ************************************************/
    @RequestMapping(value = "/hello8/{name}", method = RequestMethod.DELETE)
    public String hello8(@PathVariable String name){
        return "delete: name = " + name;
    }
}

